package com.cskaoyan.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
public class CustomAspect {

    //通知方法名可以自定义
    public void before(JoinPoint joinPoint) {
        System.out.println("正道的光");
        //代理对象
        Object aThis = joinPoint.getThis();
        System.out.println(aThis);
        //委托类（目标类）对象
        Object target = joinPoint.getTarget();
        System.out.println(target);

        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println(name);
        if (name.contains("login") || name.contains("logout")) {

        }

        Object[] args = joinPoint.getArgs();
        System.out.println(Arrays.asList(args));
    }

    //可以对形参做修饰
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        if ("sayHello".equals(joinPoint.getSignature().getName())) {
            args[1] = "张*玉";
        }
        Object proceed = joinPoint.proceed(args);
        return proceed;
    }

}
